package nl.boektrust.btsoep.client;

import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class EditBookVerticalPanelSimpleTextboxInteger extends EditBookVerticalPanelSimpleTextbox  implements BlurHandler,  ClickHandler, ValueChangeHandler<String> {
	int value;
	EditBookDialog parentDialog;
	//String value;
	
	
	public EditBookVerticalPanelSimpleTextboxInteger(String label, EditBookDialog parentDialog) {
		super(label, parentDialog);
		if (theLabel == null) {
			System.err.println("EditBookVerticalPanelSimpleTextboxInteger:theLabel is null");
		} 
		setTextBoxValue(value);

		theTextBox.addValueChangeHandler(this);
		setToDisplayMode();
		this.parentDialog = parentDialog;
	}

	public EditBookVerticalPanelSimpleTextboxInteger() {
		this("", null);
	}

	void setTextBoxValue(int theValue) {
		theTextBox.setValue(new Integer((int)value).toString());
		value = theValue;
	}

	public int getIntValue() {
		int value = -1;
		if (theTextBox != null) {
			if (theTextBox.getText().length() > 0) {
				try {
					value = new Integer(theTextBox.getText()).intValue();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return value;
	}

	@Override
	void setToEditMode() {
		if (value == -1) theTextBox.setValue(""); else setTextBoxValue(value);
		if (displayPanel != null) displayPanel.setVisible(false);
		if (editPanel != null) editPanel.setVisible(true);
		theTextBox.setFocus(true);

	}
	@Override
	void setToDisplayMode() {
		if (displayPanel != null) displayPanel.setVisible(true);
		if (editPanel != null) editPanel.setVisible(false);
		if (value >= 0) {
			theLabel.setText(labelFormatter(value)); 
		} else {
			theLabel.setText("nog niet ingevuld");
		}
	}

	
	/**
	 * @param value the value to set
	 */
	public void setValue(int theValue) {
		value = theValue;
		if (theLabel == null) System.err.println("EditBookVerticalPanelSimpleTextboxInteger:setValue(): theLabel is null");
		setTextBoxValue(value);
		if (value == -1) {
			theLabel.setText("nog niet ingevuld");
		} else {
			theLabel.setText(labelFormatter(value));
		}
	}


	public void onClick(ClickEvent event) {
		if ((Label)event.getSource() == theLabel) {
			this.toggleEditMode();
		}
		// TODO Auto-generated method stub
		
	}
	
	public void valueHasChanged(int newValue) {}

	public void onValueChange(ValueChangeEvent<String> event) {
		this.setValue(new Integer(theTextBox.getValue()).intValue());
		if (parentDialog != null) parentDialog.panelHasChanged();
		this.toggleEditMode();
		valueHasChanged(this.value);
		
	}


	public void onBlur(BlurEvent event) {
		// TODO Auto-generated method stub
		setToDisplayMode();
		
	}

}
